package win.doyto.i18n.common;

import org.apache.commons.lang3.StringUtils;
import win.doyto.query.core.Dialect;

import static win.doyto.i18n.module.i18n.I18nView.GROUP_FORMAT;

/**
 * HsqldbDialact
 *
 * @author f0rb on 2020-02-18
 */
public class HsqldbDialect implements Dialect, TranslationTableDialect {
    @Override
    public String buildPageSql(String sql, int limit, long offset) {
        return sql + " LIMIT " + limit + (sql.startsWith("SELECT") ? " OFFSET " + offset : "");
    }

    @Override
    public String buildTranslationTableDDL() {
        return StringUtils.join(new String[]{
                "CREATE TABLE",
                GROUP_FORMAT,
                "(",
                "    id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,",
                "    label VARCHAR(100) NOT NULL,",
                "    defaults VARCHAR(1000) DEFAULT '' NOT NULL,",
                "    memo VARCHAR(200) DEFAULT '',",
                "    valid BIT(1) DEFAULT TRUE NOT NULL,",
                "    CONSTRAINT uniq_" + GROUP_FORMAT + "_label UNIQUE (label)",
                ")"
        }, " ");
    }

}
